<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/5 0005
 * Time: 11:05
 */

namespace app\common\model;


use think\Exception;
use think\facade\Request;

class Goods extends BaseModel
{
    /**
     * 关联分类
     * @return \think\model\relation\BelongsTo
     */
    public function category() {
        return $this->belongsTo('Category');
    }

    /**
     * 管理获取相册图片
     * @return \think\model\relation\HasMany
     */
    public function photoAlbumImages() {
        return $this->hasMany('GoodsImages')->where('type', '=', 1)->order('sort asc, id desc');
    }

    /**
     * 管理获取详情图片
     * @return \think\model\relation\HasMany
     */
    public function contentImages() {
        return $this->hasMany('GoodsImages')->where('type', '=', 2)->order('sort asc, id desc');
    }

    /**
     * 获取商品列表
     * @param array $where
     * @param string $keyword
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public static function getList($where = [], $keyword = '') {
        return self::with(['category', 'photoAlbumImages'])->where($where)->when($keyword, function ($query) use ($keyword) {
            $query->where('name', '=', $keyword);
        })->order('sort asc, id desc')->paginate(input('param.limit', 15), false, ['query' => input('param.')]);
    }

    /**
     * 获取推荐商品，最多10条
     * @return array|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getRecommendList() {
        return self::with('photoAlbumImages')->where('recommend', '=', 1)->order('sort asc, id desc')->limit(10)->select();
    }

    /**
     * 获取首页热销商品，避免重复，只获取没有设为推荐的商品
     * @return array|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getHot() {
        return self::with('photoAlbumImages')->where('recommend', '=', 0)->order('sales desc, sort asc, id desc')->limit(30)->select();
    }

    /**
     * 创建或者更新商品
     * @param $param
     * @param int $id
     * @return \think\response\Json
     */
    public static function createOrUpdateGoods($param, $id = 0) {
        try {
            if ($id > 0) {
                // 编辑
                $goods = self::get($id);
                if (!$goods) {
                    throw new Exception('商品不存在');
                }
                $goods->save($param);
            } else {
                // 添加
                self::create($param);
            }
            return return_data('success');
        } catch (Exception $e) {
            return return_data($e->getMessage(), 1);
        }
    }

    /**
     * 修改商品排序
     * @param $id
     * @param $sort
     * @return \think\response\Json
     */
    public static function saveSort($id, $sort){
        try {
            if ($id > 0) {
                // 编辑
                $goods = self::get($id);
                if (!$goods) {
                    throw new Exception('商品不存在');
                }
                $goods->save(['sort' => $sort]);
            }
            return return_data('修改成功');
        } catch (Exception $e) {
            return return_data($e->getMessage(), 1);
        }
    }
}